package cq.com.cn.servlet;

import cq.com.cn.model.UserModel;
import cq.com.cn.service.UserService;
import cq.com.cn.utils.AjaxUtils;
import cq.com.cn.utils.Constants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/login.do")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //参数处理
        String loginName=req.getParameter("loginName");
        String pwd=req.getParameter("pwd");
        //业务处理
        UserService userService=new UserService();
        UserModel user = userService.login(loginName,pwd);
        if(user==null){
            AjaxUtils.showInfo(resp,"登录失败，请重新登录!","/view/login.html");
        }else{
            req.getSession().setAttribute(Constants.SEESIONKEY,user);
            AjaxUtils.showInfo(resp,"登录成功!","/view/bookstore.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
